<template>
  <el-dialog
    :title="!dataForm.sid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品货号" prop="sno">
      <el-input v-model="dataForm.sno" placeholder="商品货号"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="sname">
      <el-input v-model="dataForm.sname" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="分类" prop="tid">
      <el-input v-model="dataForm.tid" placeholder="分类"></el-input>
    </el-form-item>
    <el-form-item label="销售价" prop="sprice">
      <el-input v-model="dataForm.sprice" placeholder="销售价"></el-input>
    </el-form-item>
    <el-form-item label="库存" prop="stock">
      <el-input v-model="dataForm.stock" placeholder="库存"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="bsort">
      <el-input v-model="dataForm.bsort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="关键字" prop="keyword">
      <el-input v-model="dataForm.keyword" placeholder="关键字"></el-input>
    </el-form-item>
    <el-form-item label="所属商户" prop="uid">
      <el-input v-model="dataForm.uid" placeholder="所属商户"></el-input>
    </el-form-item>
    <el-form-item label="是否上架" prop="status1">
      <el-input v-model="dataForm.status1" placeholder="是否上架"></el-input>
    </el-form-item>
    <el-form-item label="是否共享" prop="status2">
      <el-input v-model="dataForm.status2" placeholder="是否共享"></el-input>
    </el-form-item>
    <el-form-item label="是否免费运" prop="status3">
      <el-input v-model="dataForm.status3" placeholder="是否免费运"></el-input>
    </el-form-item>
    <el-form-item label="销售价" prop="salesPrice">
      <el-input v-model="dataForm.salesPrice" placeholder="销售价"></el-input>
    </el-form-item>
    <el-form-item label="成本价" prop="costPrice">
      <el-input v-model="dataForm.costPrice" placeholder="成本价"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          sid: 0,
          sno: '',
          sname: '',
          tid: '',
          sprice: '',
          stock: '',
          status: '',
          bsort: '',
          keyword: '',
          uid: '',
          status1: '',
          status2: '',
          status3: '',
          salesPrice: '',
          costPrice: ''
        },
        dataRule: {
          sno: [
            { required: true, message: '商品货号不能为空', trigger: 'blur' }
          ],
          sname: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          tid: [
            { required: true, message: '分类不能为空', trigger: 'blur' }
          ],
          sprice: [
            { required: true, message: '销售价不能为空', trigger: 'blur' }
          ],
          stock: [
            { required: true, message: '库存不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          bsort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          keyword: [
            { required: true, message: '关键字不能为空', trigger: 'blur' }
          ],
          uid: [
            { required: true, message: '所属商户不能为空', trigger: 'blur' }
          ],
          status1: [
            { required: true, message: '是否上架不能为空', trigger: 'blur' }
          ],
          status2: [
            { required: true, message: '是否共享不能为空', trigger: 'blur' }
          ],
          status3: [
            { required: true, message: '是否免费运不能为空', trigger: 'blur' }
          ],
          salesPrice: [
            { required: true, message: '销售价不能为空', trigger: 'blur' }
          ],
          costPrice: [
            { required: true, message: '成本价不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.sid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.sid) {
            this.$http({
              url: this.$http.adornUrl(`/product/shop/info/${this.dataForm.sid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sno = data.shop.sno
                this.dataForm.sname = data.shop.sname
                this.dataForm.tid = data.shop.tid
                this.dataForm.sprice = data.shop.sprice
                this.dataForm.stock = data.shop.stock
                this.dataForm.status = data.shop.status
                this.dataForm.bsort = data.shop.bsort
                this.dataForm.keyword = data.shop.keyword
                this.dataForm.uid = data.shop.uid
                this.dataForm.status1 = data.shop.status1
                this.dataForm.status2 = data.shop.status2
                this.dataForm.status3 = data.shop.status3
                this.dataForm.salesPrice = data.shop.salesPrice
                this.dataForm.costPrice = data.shop.costPrice
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/shop/${!this.dataForm.sid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'sid': this.dataForm.sid || undefined,
                'sno': this.dataForm.sno,
                'sname': this.dataForm.sname,
                'tid': this.dataForm.tid,
                'sprice': this.dataForm.sprice,
                'stock': this.dataForm.stock,
                'status': this.dataForm.status,
                'bsort': this.dataForm.bsort,
                'keyword': this.dataForm.keyword,
                'uid': this.dataForm.uid,
                'status1': this.dataForm.status1,
                'status2': this.dataForm.status2,
                'status3': this.dataForm.status3,
                'salesPrice': this.dataForm.salesPrice,
                'costPrice': this.dataForm.costPrice
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
